// JavaScript Document
(function(doc, win) {
	var docEl = doc.documentElement,
		resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
		recalc = function() {
			var clientWidth = docEl.clientWidth;
			if(!clientWidth) return;
			if(clientWidth >= 740) {
				clientWidth = 740;
			}
			if(clientWidth <= 320) {
				clientWidth = 320;
			}
			docEl.style.fontSize = 100 * (clientWidth / 320) + 'px';
		};
	if(!doc.addEventListener) return;
	win.addEventListener(resizeEvt, recalc, false);
	doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);

/*================================================================*/

//PC下 不支持Tap 事件，兼容现有代码的tap方法
!(function(doc) {

	var isSupportTouch = "ontouchend" in document ? true : false;

	if(!isSupportTouch) {

		$(document).on('click', function(ev) {

			$(ev.target).trigger('tap');
		});
	}
})(document);

/**
 * 滚动、
 * @param  {number} dist   [description]
 * @param  {Object} option options
 * @return {null}        [description]
 */
$.fn.scrollTo = function(dist, option) {

	dist = Number(dist);
	if(!dist) {
		dist = 0;
	}

	var _default = {
		fn: function(f, d, sT, t) {

			var dt = new Date().getTime() - sT;

			return f + (d - f) * dt / t;
		},

		timing: 500
	}

	option = $.extend(_default, option);

	var that = this;
	var startPostion = that.scrollTop();

	var startTime = new Date().getTime();

	var step = function() {

		setTimeout(function() {

			var position = option.fn(startPostion, dist, startTime, option.timing);

			that.scrollTop(position);

			if(option.timing <= new Date().getTime() - startTime) {
				that.scrollTop(dist);
				return;
			} else {
				step();
			}

		}, 16);
	}

	step();
};